package com.cfs.vo;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.util.MyMapAdapter;

/**
 * 分页工具类
 * 
 * Client 端只需设置两个参数 page rows, 默认分别为 1 和 10
 * 
 * @author SDZJ
 * 
 */
@XmlRootElement(name = "Pager")
public class Pager implements Serializable{
	
	private static final long serialVersionUID = 4370166174568919711L;

	/**
	 * page 当前页
	 */
	private Integer page = 1;
	
	/**
	 * rows 每页显示的行数
	 */
	private Integer rows = 10;
	
	/**
	 * first 每页的第一行
	 */
	private Integer first;
	
	/**
	 * last 每页的最后一行
	 */
	private Integer last;
	
	/**
	 * 总页数
	 */
	private Integer totalP;
	
	/**
	 * 总行数
	 */
	private Integer totalR;

//	private Map<String,String> map;
	
	
	/**
	 * 在条件查询的时候使用
	 * @param paramList
	 * @return
	 */
	public Object[] queryParam(List<Object> paramList) {
		if(paramList.size()==0){
			Object[] params = new Object[2];
			params[0] = this.getPage()*this.getRows();
			params[1] = (this.getPage()-1)*this.getRows()+1;
			return params;
		}else{
//			Object[] params = new Object[paramList.size()*2 + 2];
//			int px = 0;
//			for(Object o : paramList){
//				params[px++] = o;
//			}
//			params[px++] = this.getLast();
//			for(Object o : paramList){
//				params[px++] = o;
//			}
//			params[px] = this.getFirst();
			
			Object[] params = new Object[paramList.size() + 2];
			int px = 0;
			for(Object o : paramList){
				params[px++] = o;
			}
			params[px++] = this.getLast();
			params[px] = this.getFirst();
			return params;
		}
		
	}

	public Pager() {
	}

	public Pager(Integer page, Integer rows) {
		this.page = page;
		this.rows = rows;
	}

	public Integer getFirst() {
		return first;
	}

	public void setFirst(Integer first) {
		this.first = first;
	}

	public Integer getLast() {
		return last;
	}

	public void setLast(Integer last) {
		this.last = last;
	}

	public Integer getTotalP() {
		return totalP;
	}

	public void setTotalP(Integer totalP) {
		this.totalP = totalP;
	}

	public Integer getTotalR() {
		return totalR;
	}

	public void setTotalR(Integer totalR) {
		this.totalR = totalR;
	}

	/**
	 * 设置总行数，并计算其它变量
	 * @param totalR
	 */
	public void resetTotalR(Integer totalR) {
		this.totalR = totalR > 0 ? totalR : 0;
		rows = rows > 0 ? rows : 10;
		this.totalP =  (this.totalR + rows - 1)/rows < 1 ? 0 : (this.totalR + rows - 1)/rows ;
		page = page < 1 ? 1 : (page > totalP ? totalP : page);
		if(totalP == 0) {
			page = 0;
		}
		first = (page * rows - rows) >= 1 ? (page * rows - rows) : 0;
		last = page * rows > totalR ? totalR : page * rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
		this.last = this.page*this.rows;
		this.first = this.last - this.rows;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
		this.last = this.page*this.rows;
		this.first = this.last - this.rows;
	}

	public Integer getRows() {
		return rows;
	}

}
